package com.kukudexin.demo.config;

import com.alibaba.druid.wall.WallFilter;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.source.ClassPathSourceFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.sql.Connection;

/**
 * @author: 德鑫
 * @version:
 * @since: 2021/4/27
 */
@Configuration
public class ActiveRecordPluginConfig {
    @Bean
    public ActiveRecordPlugin initActiveRecordPlugin(){
        DruidPlugin druidPlugin = new DruidPlugin("jdbc:mysql://localhost/jFinal_test", "root", "333");
        // 加强数据库安全
        WallFilter wallFilter = new WallFilter();
        wallFilter.setDbType("mysql");
        druidPlugin.addFilter(wallFilter);
        druidPlugin.addFilter(wallFilter);

        druidPlugin.start();

        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.setTransactionLevel(Connection.TRANSACTION_READ_COMMITTED);
        arp.setShowSql(false);
        arp.getEngine().setSourceFactory(new ClassPathSourceFactory());
        // 必须手动调用start
        arp.start();
        return arp;
    }
}
